﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using QuanLyDiem.models;
using QuanLyDiem.Controllers;
namespace QuanLyDiem.DanhMuc
{
    public partial class frmMonHoc : DevExpress.XtraEditors.XtraForm
    {
        public frmMonHoc()
        {
            InitializeComponent();
        }

        private void MonHoc_Load(object sender, EventArgs e)
        {
            Context db = new Context();
            gridView1.Config(true, false, false, false, false, false, false, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            monHocBindingSource.DataSource = db.MonHocs;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var monhoc = gridView1.GetFocusedRow() as MonHoc;
            if (monhoc == null) return;
            txtmamon.Text = monhoc.Ky_hieu;
            txttenmon.Text = monhoc.Ten_mon;

        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Enabled = btnxoa.Enabled = false;
            txtmamon.Text = txttenmon.Text = "";
            txttenmon.Focus();
        }

        private void btnthuchien_Click(object sender, EventArgs e)
        {
            try
            {
                Context db = new Context();
                var monhoc = new MonHoc()
                {
                    Ky_hieu = txtmamon.Text,
                    Ten_mon = txttenmon.Text
                };
                db.MonHocs.InsertOnSubmit(monhoc);
                db.SubmitChanges();
                monHocBindingSource.DataSource = db.MonHocs;
                gridView1.BestFitAllColumns();
                btnsua.Enabled = btnxoa.Enabled = true;
 
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
            
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            try
            {
                Context db = new Context();
                var monhoc = gridView1.GetFocusedRow() as MonHoc;
                monhoc.Ky_hieu = txtmamon.Text;
                monhoc.Ten_mon = txttenmon.Text;
                var monhoc1 = db.MonHocs.SingleOrDefault(a => a.ID_mon == monhoc.ID_mon);
                monhoc1.Ky_hieu = monhoc.Ky_hieu;
                monhoc1.Ten_mon = monhoc.Ten_mon;
                db.SubmitChanges();
                monHocBindingSource.DataSource = db.MonHocs;
                gridView1.BestFitAllColumns();
            }
            catch (Exception)
            
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            try
            {
                Context db = new Context();
                var monhoc = gridView1.GetFocusedRow() as MonHoc;
                var monhoc1 = db.MonHocs.SingleOrDefault(a => a.ID_mon == monhoc.ID_mon);
                db.MonHocs.DeleteOnSubmit(monhoc1);
                db.SubmitChanges();
                monHocBindingSource.DataSource = db.MonHocs;
                gridView1.BestFitAllColumns();
 
            }

            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}